import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DOM4JReader {

	static List<String> idList = new ArrayList<String>();
	static List<String> TypeList = new ArrayList<String>();

	public static void main(String args[]) throws Exception {
		File file = new File("d:" + File.separator + "output.xml");
		String viewName = "root.";
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file); // 读取XML文件
		// JDOM操作的时候是要取得根节点
		Element root = doc.getRootElement();
		StringBuffer sb = new StringBuffer();

		getIdAttribute(root);
		//全局变量
		parse1(viewName, sb);
		//局部变量
//		parse2(viewName, sb);

	}

	private static void parse1(String viewName, StringBuffer sb)
			throws FileNotFoundException {
		for (int i = 0; i < idList.size(); i++) {
   
			sb.append("private ")
					.append(TypeList.get(i))
					.append(" " + autoTransform(idList.get(i))
							+ autoType(TypeList.get(i)) + ";\n");
		}
		for (int i = 0; i < idList.size(); i++) {
			String s = "=(" + TypeList.get(i) + ")" + viewName
					+ "findViewById(R.id." + idList.get(i) + ");\n";
			sb.append(autoTransform(idList.get(i)) + autoType(TypeList.get(i)))
					.append(s);
		}
		System.out.println(sb.toString());
		File fil = new File("d:" + File.separator + "output.txt");

		PrintWriter writer = new PrintWriter(fil);
		writer.write(sb.toString());
		writer.flush();
	}

	private static void parse2 (String viewName, StringBuffer sb)
			throws FileNotFoundException {
		for (int i = 0; i < idList.size(); i++) {
			String s = "=(" + TypeList.get(i) + ")" + viewName
					+ "findViewById(R.id." + idList.get(i) + ");\n";
			sb.append(TypeList.get(i)+"  "+autoTransform(idList.get(i)) + autoType(TypeList.get(i)))
					.append(s);
		}
		System.out.println(sb.toString());
		File fil = new File("d:" + File.separator + "output.txt");

		PrintWriter writer = new PrintWriter(fil);
		writer.write(sb.toString());
		writer.flush();
	}

	private static void getIdAttribute(Element root) {
		// 现在应该根据根节点找到全部的子节点，linkman
		Iterator iter = root.elementIterator();
		while (iter.hasNext()) {
			Element sub = (Element) iter.next();
			getIdAttribute(sub);
		}
		if (root.attributeValue("id") != null) {
			idList.add(root.attributeValue("id").split("/")[1]);
			TypeList.add(root.getName());
		}

	}

	private static String autoTransform(String str) {

		String[] arrayStr = str.split("_");
		String transform = arrayStr[0];
		for (int i = 1; i < arrayStr.length; i++) {
			arrayStr[i] = autoToFirstUpcase(arrayStr[i]);
			transform += arrayStr[i];
		}

		return transform;
	}

	private static String autoToFirstUpcase(String str) {
		return (str.charAt(0) + "").toUpperCase() + str.substring(1);
	}

	private static String autoType(String type) {
		String resultStr = type.replaceAll("[a-z]", "");
		return resultStr;

	}

}
